//
//  TextCell.swift
//  YueChat
//
//  Created by IMac on 2024/9/21.
//

import UIKit

class TextCell: BaseChatCell, TTTAttributedLabelDelegate {
    
    @IBOutlet weak var msg: TTTAttributedLabel!
    
    @IBOutlet weak var quoteView: UIView!
    
    @IBOutlet weak var quoImgWithMargn: NSLayoutConstraint!
    @IBOutlet weak var playImg: UIImageView!
    @IBOutlet weak var quoImg: UIImageView!
    @IBOutlet weak var quoSubLab: UILabel!
    @IBOutlet weak var quoNameLab: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        // 设置 icon 的圆角和剪裁
        //        self.icon.layer.masksToBounds = true
        //        self.icon.layer.cornerRadius = 2
        self.msgView.backgroundColor = UIColor.clear
        
        
        
        guard let msgView = msgView  else {
            return
        }
        msgView.layer.masksToBounds = true
        msgView.layer.cornerRadius = 0
        
        guard let quoteView = quoteView else  {
            return
        }
        
        quoteView.layer.masksToBounds = true
        quoteView.layer.cornerRadius = 12
        
        // 添加手势识别器到 quoteView
        quoteView.isUserInteractionEnabled = true
        //        self.quoteView.addGestureRecognizer(self.quoteTapGes)
        
        // 设置 icon 的用户交互
        //        self.icon.isUserInteractionEnabled = true
        //        self.icon.addGestureRecognizer(self.iconTapGes)
        //        self.icon.addGestureRecognizer(self.longIconGes)
        
        // 设置 msg 的用户交互
        self.msg.isUserInteractionEnabled = true
        self.msg.addGestureRecognizer(self.longGes)
        
        self.msg.delegate = self
        
        // 设置 mulView 的隐藏状态
        self.mulView.isHidden = true
        
        // 添加手势识别器到 mulView
        self.mulView.addGestureRecognizer(self.mulTapGes)
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    
    // 计算单元格的高度
    override func baseCellHeight(message: DMCCMessage, chatType: Bool, mulSelect: Bool, isMySend: Bool) {
        
        guard message.msgCellHeight <= 0 else { return }
        
        let textWidth = 140.5
        
        let content = message.content as! OsnTextMessageContent
        let digest = content.data
//        let digest = content.data.count == 0 ? content.data : "\(String(describing:content.data))\n\(String(describing: message.extra))"
        
        var height = cellTextHeight(digest, font: 16, textWith: UIScreen.main.bounds.width - textWidth)
        
        
        let width = cellTextWidth(for: digest, font: 16, textHeight: height)
        height += 44
        
        message.msgCellWidth = Int(width)
        message.msgCellHeight = Int(height)
        
        let quote = content.quoteInfo
        if quote != nil && quote!.userId.count > 0 {
            message.msgCellHeight += 70
        }
    }
    
    
    // 设置消息
    override func setMessage(_ message: DMCCMessage) {
        super.setMessage(message)
        let content = message.content as! OsnTextMessageContent
        let digest = content.data
//        let digest = message.extra.isEmpty ? message.digest : "\(message.digest)\n\(message.extra)"
//        let digest = message.extra.count == 0 ? content.data : "\(String(describing: content.data))\n\(String(describing: message.extra))"
        
//        if let text = message.content as! OsnTextMessageContent {
//            if text.quoteInfo.userId.count > 0 {
//                self.quoNameLab.text = text.quoteInfo.userDisplayName
//                self.quoSubLab.text = text.quoteInfo.messageDigest
////                self.quoBottomMargn.constant = 80
//                self.quoteView.isHidden = false
//            } else {
//                self.quoteView.isHidden = true
////                self.quoBottomMargn.constant = 15
//            }
//        }
        
        if message.getFrom() == myInfo.userId {
            self.msg.textColor = .white
            self.msg.setText(digest)

        } else {
            self.msg.textColor = .black
            self.msg.setText(digest)
            
            if chatType == 1 {
                self.headImgBG.isHidden = false

              
            } else {
                self.headImgBG.isHidden = true
                self.headImg.sd_setImage(with: URL(string: userInfo.portrait), placeholderImage: SDImageDefault)
            }
            
        }
        
        
        if let text = message.content as? OsnTextMessageContent,
           let quoteInfo = text.quoteInfo {
            if quoteInfo.userId.count > 0 {
//                if message.extra.isEmpty {
//
//                    quoTop.constant = -26
//                } else {
//                    
//                    quoTop.constant = 8
//                }
                
                quoNameLab.text = "\(quoteInfo.userDisplayName):"
                quoSubLab.text = ""
                
                quoteView.isHidden = false
                playImg.isHidden = true
                
                var msg = gosnMessage.getMessageFromHasho(quoteInfo.messageHasho)
                if msg == nil {
                    msg = gosnMessage.getMessageFromHash(quoteInfo.messageHasho)
                }
                
//                    .getMessageByHashO(text.quoteInfo.messageHasho) ?? gimService.queryMessage(text.quoteInfo.messageHasho)
                
                if   msg!.content is OsnImageMessageContent {
//                    handleImageContent(imgContent)
                } else if msg!.content is OsnVideoMessageContent {
//                    handleVideoContent(videoContent)
                } else {
                    quoNameLab.text = quoteInfo.messageDigest
                    quoImgWithMargn.constant = 0
                }
            } else {
                guard quoteView != nil else {
                    return
                }
                
                quoteView.isHidden = true
//                quoBottomMargn.constant = 15
            }
        }

                    

        // 设置 nickNameLabel 的 frame
//        self.nickNameLabel.frame = CGRect(x: 62, y: message.msgCellHeight + 4, width: Int(UIScreen.main.bounds.width), height: 20)

        // 设置 errorImg 的约束
        
//        self.errorImg.snp.makeConstraints { make in
//            make.width.height.equalTo(20)
//            make.left.equalTo(self.msg.snp.left).offset(-15)
//            make.top.equalTo(self.msg.snp.top).offset(-5)
//        }

//        if self.isMulSelect {
//            self.leftMsg.constant = 50
//            self.mulView.isHidden = false
//            self.mulBtn.isSelected = message.isMulSelect
//        } else {
//            self.leftMsg.constant = 15
//            self.mulView.isHidden = true
//        }
        
    }
    
}
